<?php
/**
 * Minhlog Lucene搜索类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Minh
 * @package    Minh_Search
 * @copyright  Copyright (c) 2009-2010 Minh.Shu Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Lucene.php 24 2010-02-16 09:19:13Z Minh.Shu $
 */

class Minh_Search_Lucene
{
    private $_indexDir;
    private static $_index;

    public function  __construct()
    {
        $this->_indexDir = APPLICATION_PATH . '/indexs/lucene';    //搜索索引目录
        if (! file_exists($this->_indexDir)) {
            @mkdir($this->_indexDir, 0777);
        }
        //设置搜索词法分析器为Utf8
        Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
        Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
    }

    /**
     * 建立/更新索引入口
     */
    public function build()
    {
        if ($handle = opendir($this->_indexDir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != ".." && $file != "read.lock.file" && $file != ".svn") {
                    $this->update();
                    break;
                }
            }
            $this->create();
            closedir($handle);
        }
    }

    /**
     * 创建索引方法
     * @return object
     */
    public function create()
    {
        self::$_index = Zend_Search_Lucene::create($this->_indexDir);
        self::$_index->setFormatVersion(2);
        return;
    }

    /**
     * 更新索引方法
     * @return object
     */
    public function update()
    {
        self::$_index = Zend_Search_Lucene::open($this->_indexDir);
        self::$_index->setFormatVersion(2);
        return;
    }

    /**
     * 将索引文档添加到索引中
     * @param array $document = array(
     *                  array(
     *                      'type'  => $type,
     *                      'key'   => $key,
     *                      'value' => $value
     *                  ),
     *                  .....
     *               );
     */
    public function addDoc($document)
    {
        $doc = new Zend_Search_Lucene_Document();
        foreach ($document as $oneDoc) {
            $doc->addField(Zend_Search_Lucene_Field::$oneDoc['type']($oneDoc['key'], $oneDoc['value'], 'utf-8'));
        }
        self::$_index->addDocument($doc);
    }

    /**
     * 优化索引
     * @return
     */
    public function optimize()
    {
        return self::$_index->optimize();
    }

    /**
     * 提交索引
     * @return
     */
    public function commit()
    {
        return self::$_index->commit();
    }

    /**
     * 搜索API
     * @param string $keyword
     * @return
     */
    public function search($keyword)
    {
        self::$_index = Zend_Search_Lucene::open($this->_indexDir);

        self::$_index->optimize();

        /**
         * 实现单词多条件模糊搜索
         * @todo 多词搜索
         */
        $query = new Zend_Search_Lucene_Search_Query_Boolean();

        if(strlen($keyword) < 3){
            /* 文章内容搜索 */
            $subquery1 = new Zend_Search_Lucene_Search_Query_Phrase();
            $subquery1->addTerm(new Zend_Search_Lucene_Index_Term($keyword,'articleContent'));
            /* 文章摘要搜索 */
            $subquery2 = new Zend_Search_Lucene_Search_Query_Phrase();
            $subquery2->addTerm(new Zend_Search_Lucene_Index_Term($keyword,'articleSummary'));
            /* 文章标题搜索 */
            $subquery3 = new Zend_Search_Lucene_Search_Query_Phrase();
            $subquery3->addTerm(new Zend_Search_Lucene_Index_Term($keyword,'articleTitle'));
            /* 文章标签搜索 */
            $subquery4 = new Zend_Search_Lucene_Search_Query_Phrase();
            $subquery4->addTerm(new Zend_Search_Lucene_Index_Term($keyword,'tag'));
        }else{
            /* 文章内容搜索 */
            $pattern1 = new Zend_Search_Lucene_Index_Term($keyword.'*','articleContent');
            $subquery1 = new Zend_Search_Lucene_Search_Query_Wildcard($pattern1);
            /* 文章摘要搜索 */
            $pattern2 = new Zend_Search_Lucene_Index_Term($keyword.'*','articleSummary');
            $subquery2 = new Zend_Search_Lucene_Search_Query_Wildcard($pattern2);
            /* 文章标题搜索 */
            $pattern3 = new Zend_Search_Lucene_Index_Term($keyword.'*','articleTitle');
            $subquery3 = new Zend_Search_Lucene_Search_Query_Wildcard($pattern3);
            /* 文章标签搜索 */
            $pattern4 = new Zend_Search_Lucene_Index_Term($keyword.'*','tag');
            $subquery4 = new Zend_Search_Lucene_Search_Query_Wildcard($pattern4);
        }

        $query->addSubquery($subquery1);
        $query->addSubquery($subquery2);
        $query->addSubquery($subquery3);
        $query->addSubquery($subquery4);

        $hits = self::$_index->find($query);

        return $hits;
    }
}